package com.buyouchina.utils;

import java.util.ArrayList;
import java.util.List;


import com.buyouchina.bean.area.Province;
import com.buyouchina.service.area.ProvinceService;

public class ProvinceUtil {

	/**
	 * 取得下一个省份的id
	 * @param provinceid
	 *            当前省份id
	 * */

	public static Integer getNextProvince(ProvinceService service,Integer provinceid) {

		Province province=service.find(provinceid);
		Province province1=null;
		if(province.getProvinceid()!=null){	//	定义为市级别的地区
			provinceid=Integer.parseInt(province.getProvinceid());
		}else if(province.getCityid()!=null){
			provinceid=Integer.parseInt(service.find(Integer.parseInt(province.getCityid())).getProvinceid());
		}
		List<Province> provinceList = new ArrayList<Province>();
		provinceList = service.list("from Province p where p.id>'" + provinceid
				+ "' and p.provinceid is null and p.cityid is null");
		if(provinceList.size()>0){
			province1 = provinceList.get(0);
			 
		}else{
			provinceList=service.list("select top 1 p from Province order by p.id desc");
			province1 = provinceList.get(0);
		}
		return province1.getId();
		
	}

	/**
	 * 取得当前省份包含所有地区的id
	 * @param provinceid
	 *            当前省份id
	 * */
	public static Integer getLastIndexOfProvince(ProvinceService service,Integer provinceid) {
		return getNextProvince(service,provinceid) - 1;
	}

	private static Integer getNextCity(ProvinceService service,Integer provinceid) {

		Province province=service.find(provinceid);
		Province province1=null;
		if(province.getCityid()!=null){
			provinceid=Integer.parseInt(province.getCityid());
		}
		List<Province> provinceList = new ArrayList<Province>();
		provinceList = service.list("from Province p where p.id>'" + provinceid
				+ "' and p.provinceid is not null");
		if(provinceList.size()>0){
			province1 = provinceList.get(0);
		}else{
			provinceList=service.list("select top 1 p from Province order by p.id desc");
			province1 = provinceList.get(0);
		}
		return province1.getId();
	}

	/**
	 * 取得当前省份包含所有地区的id
	 * @param provinceid
	 *            当前省份id
	 * */
	public static Integer getLastIndexOfCity(ProvinceService service,Integer provinceid) {
		return getNextCity(service,provinceid) - 1;
	}

}
